#include "Messenger.h"

Messenger::Messenger(){}

Messenger::~Messenger(){}

void Messenger::insertMsg(Message * msg){
    //insert to the queue
//	cout<<"insertMSG"<<endl;
//	cout<<msg.toString()<<endl;
    if (msg->type == Message::SYSTEM){
        sys_inbox.push(msg);
//        cout<<"message : "<<msg.toString()<<" inserted to System inbox"<<endl;
    }
    else{
        inbox.push(msg);
//        cout<<"message : "<<msg.toString()<<" inserted to inbox"<<endl;
    }

}


void Messenger::popMsg(){

    if (this->inbox.size()==0 && this->sys_inbox.size()==0){
        return;
    }

    //some messages are in the inbox:
    Message * msg;
    if (sys_inbox.size()!=0)
        msg = sys_inbox.pop();
    else
        msg = inbox.pop();

    //cout<<"pop: handle msg:"<<endl;
    //cout<<msg.toString()<<endl;
    //handle the message:
    switch(msg->type){
    case Message::SYSTEM:
        handleSysMsg(msg);
        break;
    case Message::RT:
        handleRTMsg(msg);
        break;
    case Message::MSG:
        handleMsg(msg);
        break;
    }
}

int Messenger::get_inbox_size(){
	return inbox.size();
}
int Messenger::get_sys_inbox_size(){
	return sys_inbox.size();
}

void Messenger::inbox_clear(){
	inbox.clear();
}

void Messenger::sys_inbox_clear(){
	sys_inbox.clear();
}
